package com.ssau.tnp.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import math.Matrix;

public class ThreadedEchoHandler implements Runnable {
    private Socket socket;

    ThreadedEchoHandler(Socket socket) {
	this.socket = socket;
    }

    @Override
    public void run() {
	try {
	    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
	    Matrix m1 = new Matrix();
	    m1 = (Matrix) in.readObject();
	    System.out.println("Data on server:\n");
	    System.out.println("\t Matrix 1:\n");
	    for (int i = 0; i < m1.getRows(); i++) {
		for (int j = 0; j < m1.getColumns(); j++) {
		    System.out.print(m1.getElement(i, j) + "  ");
		}
		System.out.println();
	    }

	    Matrix m2 = new Matrix((Matrix) in.readObject());
	    System.out.println("Data on server:\n");
	    System.out.println("\t Matrix 2:\n");
	    for (int i = 0; i < m1.getRows(); i++) {
		for (int j = 0; j < m1.getColumns(); j++) {
		    System.out.print(m1.getElement(i, j) + "  ");
		}
		System.out.println();
	    }

	    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
	    out.writeObject(m1 = m1.operation(m2));
	    out.flush();
	    out.close();

	} catch (IOException e) {
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}

    }
}
